
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#include "GRTParamVertexPrimitive.h"

#include <QLabel>
#include <QGridLayout>
#include <QRadioButton>
#include <QLineEdit>
#include <QGroupBox>

//#include "GRTCommonParameter.h"
#include "GRTGeoVertex.h"
#include "GRTGeoMesh.h"

#include "GRTScene.h"
#include "GRTMemoryLeak.h"
//constructor
GRTParamVertexPrimitive::GRTParamVertexPrimitive(QWidget* parent) : GRTBaseParamWidget(parent), triangleID(1)
{
	//Title
	_title = new QLabel("VERTEX");

	_triangleRadio[0] = new QRadioButton("Triangle 1", this);
	_triangleRadio[1] = new QRadioButton("Triangle 2", this);
	_triangleRadio[2] = new QRadioButton("Triangle 3", this);
	_triangleRadio[3] = new QRadioButton("Triangle 4", this);
	_triangleRadio[4] = new QRadioButton("Triangle 5", this);
	_triangleRadio[5] = new QRadioButton("Triangle 6", this);
	_triangleRadio[6] = new QRadioButton("Triangle 7", this);
	_triangleRadio[7] = new QRadioButton("Triangle 8", this);
	_triangleRadio[8] = new QRadioButton("Triangle 9", this);
	_triangleRadio[9] = new QRadioButton("Triangle 10", this);

	for(int i = 0; i < 10; i++)
		connect(_triangleRadio[i], SIGNAL(clicked(bool)), this, SLOT(triangleSelected(bool)));

	//group box uv
	_uvGroup = new QGroupBox("UV", this);

	//U
	_uLabel = new QLabel("U = ", _uvGroup);
	_uText = new QLineEdit(_uvGroup);

	//V
	_vLabel = new QLabel("V = ", _uvGroup);
	_vText = new QLineEdit(_uvGroup);

	//layout uvmap
	QGridLayout* layoutUV = new QGridLayout();
	layoutUV->addWidget(_uLabel, 0, 0, 1, 1);
	layoutUV->addWidget(_uText, 0, 1, 1, 1);
	layoutUV->addWidget(_vLabel, 1, 0, 1, 1);
	layoutUV->addWidget(_vText, 1, 1, 1, 1);

	_uvGroup->setLayout(layoutUV);

	//widget's layout
	_layout = new QGridLayout();

	_layout->addWidget(_title, 0, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_triangleRadio[0], 1, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_triangleRadio[1], 2, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_triangleRadio[2], 3, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_triangleRadio[3], 4, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_triangleRadio[4], 5, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_triangleRadio[5], 6, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_triangleRadio[6], 7, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_triangleRadio[7], 8, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_triangleRadio[8], 9, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_triangleRadio[9], 10, 0, 1, 2, Qt::AlignHCenter);
	//_layout->addWidget(_common, 1, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(_uvGroup, 11, 0, 1, 2, Qt::AlignHCenter);
	_layout->addWidget(new QWidget(), 12, 0, 1, 2);
	_layout->setRowStretch(12, 1);

	setLayout(_layout);

	//connection to change the position of the object
	connect(_uText, SIGNAL(textEdited(const QString&)), this, SLOT(setUVMap()));
	connect(_vText, SIGNAL(textEdited(const QString&)), this, SLOT(setUVMap()));
}

//destructor
GRTParamVertexPrimitive::~GRTParamVertexPrimitive(){}

//fill all the widgets with values
void GRTParamVertexPrimitive::fillWidgets(GRTGeometry* g)
{
	hideAllRadioButton();

	//get the number of triangle
	GRTGeoVertex* v = (GRTGeoVertex*)g;
	int nbTriangle = v->getTriangleCount();
	if (nbTriangle > 10) nbTriangle = 10;
	for(int i = 0; i < nbTriangle; i++)
		_triangleRadio[i]->show();

}

void GRTParamVertexPrimitive::hideAllRadioButton()
{
	for(int i = 0; i < 10; i++)
		_triangleRadio[i]->hide();

	_triangleRadio[0]->setChecked(true);
	triangleSelected(true);
}

void GRTParamVertexPrimitive::triangleSelected(bool /*checked*/)
{
	for(int i = 0; i<10; i++)
	{
		if (_triangleRadio[i]->isChecked())
		{
			triangleID = i;
			GRTGeoVertex* v = (GRTGeoVertex*)SCENE.getSelectedObject();
			GRTGeoMesh* m = v->getMesh();
			m->setDrawSpecialTriangle(v->getIndex(), i);
			RTVector3f uv = m->getUVMap(v->getIndex(), i);
			_uText->setText(QString::number(uv.x));
			_vText->setText(QString::number(uv.y));
		}
	}
}

void GRTParamVertexPrimitive::setUVMap()
{
	RTVector3f uv(_uText->text().toFloat(), _vText->text().toFloat(), 0);
	GRTGeoVertex* v = (GRTGeoVertex*)SCENE.getSelectedObject();
	GRTGeoMesh* m = v->getMesh();
	m->setUVMap(v->getIndex(), triangleID, uv);
}